<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>flask-login | 风晴的小站</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/vuepress-reco-demo/favicon.ico">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/vuepress-reco-demo/assets/css/0.styles.b62e7f31.css" as="style"><link rel="preload" href="/vuepress-reco-demo/assets/js/app.4bb1badd.js" as="script"><link rel="preload" href="/vuepress-reco-demo/assets/js/3.dedc3bda.js" as="script"><link rel="preload" href="/vuepress-reco-demo/assets/js/1.1f77094c.js" as="script"><link rel="preload" href="/vuepress-reco-demo/assets/js/28.4eef19c0.js" as="script"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/10.da6c6615.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/11.278dedb8.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/12.8bf39f5a.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/13.42d9fb10.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/14.b12eaca7.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/15.304ef6c0.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/16.c25a4baa.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/17.73a7ed46.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/18.f46ae896.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/19.8f5f023a.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/20.b7574fce.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/21.d5991a0f.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/22.15018480.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/23.da12378e.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/24.608b9c02.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/25.344d0ef0.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/26.c216c7dd.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/27.601944e2.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/4.8e02f107.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/5.8e0dc2ab.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/6.76277f0d.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/7.c22116bf.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/8.57275b55.js"><link rel="prefetch" href="/vuepress-reco-demo/assets/js/9.638f97d3.js">
    <link rel="stylesheet" href="/vuepress-reco-demo/assets/css/0.styles.b62e7f31.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>风晴的小站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2></p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>YMZ</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vuepress-reco-demo/" class="home-link router-link-active"><img src="/vuepress-reco-demo/square-avatar.png" alt="风晴的小站" class="logo"> <span class="site-name">风晴的小站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/vuepress-reco-demo/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><a href="/vuepress-reco-demo/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/blogs/考研/" class="nav-link"><i class="undefined"></i>
  考研
</a></li><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/docs/开发人员/" class="nav-link"><i class="undefined"></i>
  开发人员
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/categories/学习/" class="nav-link"><i class="undefined"></i>
  学习
</a></li><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/categories/计算机技术/" class="nav-link"><i class="undefined"></i>
  计算机技术
</a></li><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/categories/计算机语言/" class="nav-link"><i class="undefined"></i>
  计算机语言
</a></li></ul></div></div><div class="nav-item"><a href="/vuepress-reco-demo/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/Silfra" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-gitee"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><img src="/vuepress-reco-demo/square-avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    YMZ
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>16</h3> <h6 data-v-39576ba9>文章</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>12</h3> <h6 data-v-39576ba9>标签</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/vuepress-reco-demo/" class="nav-link"><i class="iconfont reco-home"></i>
  Home
</a></div><div class="nav-item"><a href="/vuepress-reco-demo/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/blogs/考研/" class="nav-link"><i class="undefined"></i>
  考研
</a></li><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/docs/开发人员/" class="nav-link"><i class="undefined"></i>
  开发人员
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      Category
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/categories/学习/" class="nav-link"><i class="undefined"></i>
  学习
</a></li><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/categories/计算机技术/" class="nav-link"><i class="undefined"></i>
  计算机技术
</a></li><li class="dropdown-item"><!----> <a href="/vuepress-reco-demo/categories/计算机语言/" class="nav-link"><i class="undefined"></i>
  计算机语言
</a></li></ul></div></div><div class="nav-item"><a href="/vuepress-reco-demo/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      Contact
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/Silfra" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-gitee"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>flask-login</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>YMZ</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2023
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">flask-login</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>YMZ</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>2023/6/24</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>flask扩展插件</span></i></div></div> <div class="theme-reco-content content__default"><p>本文介绍<code>flask-login</code>的使用，一些细微的步骤将不再阐述，您需要熟悉或了解<code>flask</code>的一些知识。</p> <p>从创建一个虚拟环境开始，本文教你如何使用<code>flask-login</code>实现用户登录。</p> <h2 id="创建虚拟环境"><a href="#创建虚拟环境" class="header-anchor">#</a> 创建虚拟环境</h2> <p>这里使用pycharm创建一个flask虚拟环境，并安装扩展插件和<code>flask-wtf</code>插件。</p> <p>需要安装的第三方库：</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>pip <span class="token function">install</span> flask-login, flask-wtf, python-dotenv, watchdog
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="custom-block tip"><p class="title"></p><p><strong>项目结构：</strong></p> <p><img src="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.assets/image-20230624212623496.png" alt="image-20230624212623496"></p></div><blockquote><p>一般说来，flask-login需要有数据库的支持，这里将数据直接前置在文件中，方便学习。</p></blockquote> <h2 id="编写逻辑"><a href="#编写逻辑" class="header-anchor">#</a> 编写逻辑</h2> <h3 id="需要引用的包"><a href="#需要引用的包" class="header-anchor">#</a> 需要引用的包</h3> <p>这里列出了整个<code>app.py</code>所需的全部第三方库。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> flask <span class="token keyword">import</span> Flask
<span class="token keyword">from</span> flask_login <span class="token keyword">import</span> LoginManager<span class="token punctuation">,</span> UserMixin<span class="token punctuation">,</span> logout_user
<span class="token keyword">from</span> werkzeug<span class="token punctuation">.</span>security <span class="token keyword">import</span> generate_password_hash
<span class="token keyword">import</span> uuid
<span class="token keyword">from</span> werkzeug<span class="token punctuation">.</span>security <span class="token keyword">import</span> check_password_hash
<span class="token keyword">from</span> wtforms <span class="token keyword">import</span> StringField<span class="token punctuation">,</span> PasswordField
<span class="token keyword">from</span> flask_wtf <span class="token keyword">import</span> FlaskForm
<span class="token keyword">from</span> wtforms<span class="token punctuation">.</span>validators <span class="token keyword">import</span> DataRequired
<span class="token keyword">from</span> flask <span class="token keyword">import</span> redirect<span class="token punctuation">,</span> request
<span class="token keyword">from</span> flask_login <span class="token keyword">import</span> login_user
<span class="token keyword">from</span> flask <span class="token keyword">import</span> render_template<span class="token punctuation">,</span> url_for
<span class="token keyword">from</span> flask_login <span class="token keyword">import</span> current_user<span class="token punctuation">,</span> login_required
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="加载密钥和初始化登陆对象"><a href="#加载密钥和初始化登陆对象" class="header-anchor">#</a> 加载密钥和初始化登陆对象</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>app<span class="token punctuation">.</span>secret_key <span class="token operator">=</span> <span class="token string">'abc'</span>

login_manager <span class="token operator">=</span> LoginManager<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 实例化登录管理对象</span>
login_manager<span class="token punctuation">.</span>init_app<span class="token punctuation">(</span>app<span class="token punctuation">)</span>  <span class="token comment"># 初始化应用</span>
login_manager<span class="token punctuation">.</span>login_view <span class="token operator">=</span> <span class="token string">'login'</span>  <span class="token comment"># 设置用户登录视图函数 endpoint，登录失败也会跳转至此</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="加载前置用户"><a href="#加载前置用户" class="header-anchor">#</a> 加载前置用户</h3> <div class="language-python line-numbers-mode"><pre class="language-python"><code>USERS <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">{</span>
        <span class="token string">&quot;id&quot;</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>  <span class="token comment"># 用户识别码，相当于主键</span>
        <span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">'lily'</span><span class="token punctuation">,</span>  <span class="token comment"># 登录用户名</span>
        <span class="token string">&quot;password&quot;</span><span class="token punctuation">:</span> generate_password_hash<span class="token punctuation">(</span><span class="token string">'123456'</span><span class="token punctuation">)</span>  <span class="token comment"># 登录密码</span>
        <span class="token comment"># 使用 sha256 加密算法将字符串变为密文，不要在数据库中存储明文密码</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        <span class="token string">&quot;id&quot;</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">'tom'</span><span class="token punctuation">,</span>
        <span class="token string">&quot;password&quot;</span><span class="token punctuation">:</span> generate_password_hash<span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        <span class="token string">&quot;id&quot;</span><span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
        <span class="token string">&quot;name&quot;</span><span class="token punctuation">:</span> <span class="token string">'ymz'</span><span class="token punctuation">,</span>
        <span class="token string">&quot;password&quot;</span><span class="token punctuation">:</span> generate_password_hash<span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="创建用户示例对象"><a href="#创建用户示例对象" class="header-anchor">#</a> 创建用户示例对象</h3> <p>用户示例对象是整个<code>flask-login</code>的关键，<code>flask-login</code>
依靠该实例对象来实现登录、验证用户等所有逻辑，因此他需要继承<code>flask-login</code>提供的用户基类方法<code>UserMixin</code>。</p> <p>实例对象用于<code>flask-login</code>创建<code>session</code>对象，不要忘记<code>flask-login</code>也是依靠<code>session</code>工作的。</p> <p><strong>该对象与数据库中存储的用户记录无关，只是创建<code>session</code>和校验用户等逻辑的工具。</strong></p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">class</span> <span class="token class-name">User</span><span class="token punctuation">(</span>UserMixin<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;用户类&quot;&quot;&quot;</span>

    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> user<span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>username <span class="token operator">=</span> user<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>password_hash <span class="token operator">=</span> user<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">&quot;password&quot;</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span><span class="token builtin">id</span> <span class="token operator">=</span> user<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span>

    <span class="token comment"># 接收明文密码，在判断逻辑中</span>
    <span class="token keyword">def</span> <span class="token function">verify_password</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> password<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;密码验证&quot;&quot;&quot;</span>
        <span class="token keyword">if</span> self<span class="token punctuation">.</span>password_hash <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
            <span class="token keyword">return</span> <span class="token boolean">False</span>
        <span class="token keyword">return</span> check_password_hash<span class="token punctuation">(</span>self<span class="token punctuation">.</span>password_hash<span class="token punctuation">,</span> password<span class="token punctuation">)</span>

    <span class="token comment"># 必须实现，否则flask-login无法判断该用户是否被验证</span>
    <span class="token keyword">def</span> <span class="token function">get_id</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;获取用户ID&quot;&quot;&quot;</span>
        <span class="token keyword">return</span> self<span class="token punctuation">.</span><span class="token builtin">id</span>

    <span class="token comment"># 必须接收id，返回对应的用户实例</span>
    <span class="token decorator annotation punctuation">@staticmethod</span>
    <span class="token keyword">def</span> <span class="token function">get</span><span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;根据用户ID获取用户实体，为 login_user 方法提供支持&quot;&quot;&quot;</span>
        <span class="token keyword">if</span> <span class="token keyword">not</span> user_id<span class="token punctuation">:</span>
            <span class="token keyword">return</span> <span class="token boolean">None</span>
        <span class="token keyword">for</span> user <span class="token keyword">in</span> USERS<span class="token punctuation">:</span>
            <span class="token keyword">if</span> user<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'id'</span><span class="token punctuation">)</span> <span class="token operator">==</span> user_id<span class="token punctuation">:</span>
                <span class="token keyword">return</span> User<span class="token punctuation">(</span>user<span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token boolean">None</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p>该对象实现了3种方法：</p> <p>实例化对象实现获取给定的用户记录的所有信息，包括用户名，加密密码等，这在后面的方法中将会用到。</p> <h4 id="verify-password"><a href="#verify-password" class="header-anchor">#</a> verify_password</h4> <p>该方法接收一个明文密码，这个密码在前端的提交表单中被获取，而前端提交的一些信息（后文将讲到）将被实例化为该对象，因此可以获得该明文密码。判断该明文密码是否与数据库中记录的用户密码匹配，即核心验证逻辑。</p> <h4 id="get-id"><a href="#get-id" class="header-anchor">#</a> get_id</h4> <p>如注释，必须实现的方法，该方法返回用户的id。</p> <h4 id="get"><a href="#get" class="header-anchor">#</a> get</h4> <p>静态方法，该方法接收一个id（数据库中记录的用户id），并从数据库中找到该用户记录，将该用户记录实例化为该对象并返回。</p> <h3 id="编写前端逻辑"><a href="#编写前端逻辑" class="header-anchor">#</a> 编写前端逻辑</h3> <h4 id="前端login页面"><a href="#前端login页面" class="header-anchor">#</a> 前端login页面</h4> <p>直接给出页面html内容（下同），你需要熟悉<code>flask-wtf</code>的一些基础用法。</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>zh<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>login<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
{% macro render_field(field) %} <span class="token comment">&lt;!-- 定义字段宏 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dt</span><span class="token punctuation">&gt;</span></span>{{ field.label }}:
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dd</span><span class="token punctuation">&gt;</span></span>{{ field(**kwargs)|safe }}
    {% if field.errors %}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span>errors</span><span class="token punctuation">&gt;</span></span>
        {% for error in field.errors %}
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>{{ error }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        {% endfor %}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
    {% endif %}
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dd</span><span class="token punctuation">&gt;</span></span>
{% endmacro %}

<span class="token comment">&lt;!-- 登录表单 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>form</span> <span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>POST<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    {{ form.csrf_token }}
    {{ render_field(form.username) }}
    {{ render_field(form.password) }}
    {% if emsg %}  <span class="token comment">&lt;!-- 如果有错误信息 则显示 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h3</span><span class="token punctuation">&gt;</span></span> {{ emsg }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h3</span><span class="token punctuation">&gt;</span></span>
    {% endif %}
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>submit<span class="token punctuation">&quot;</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>登录<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>form</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h4 id="前端index页面"><a href="#前端index页面" class="header-anchor">#</a> 前端index页面</h4> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>zh<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>index<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span><span class="token punctuation">&gt;</span></span>欢迎 {{ username }}！<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">'</span>{{ url_for(<span class="token punctuation">'</span></span> <span class="token attr-name">logout')</span> <span class="token attr-name">}}'</span><span class="token punctuation">&gt;</span></span>登出<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="后端登录逻辑"><a href="#后端登录逻辑" class="header-anchor">#</a> 后端登录逻辑</h3> <p>前端给出两种信息：</p> <ol><li>用户名</li> <li>明文密码</li></ol> <p>要使用flask-login的登录逻辑，无论如何都需要先从数据库中找到这个用户记录，如果没有这条用户记录，flask-login以及其登录对象又有什么意义呢？</p> <p>因此，需要定义一个可以直接从数据库中读取用户记录的方法，这里根据用户名查找用户记录，并返回原生用户记录。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">get_user</span><span class="token punctuation">(</span>user_name<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;根据用户名获得用户记录&quot;&quot;&quot;</span>
    <span class="token keyword">for</span> user <span class="token keyword">in</span> USERS<span class="token punctuation">:</span>
        <span class="token keyword">if</span> user<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span> <span class="token operator">==</span> user_name<span class="token punctuation">:</span>
            <span class="token keyword">return</span> user
    <span class="token keyword">return</span> <span class="token boolean">None</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>登录的逻辑允许get和post两种方法，如果是get，则直接返回登陆页面，否则验证表单进入登录逻辑。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/login'</span><span class="token punctuation">,</span> methods<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">'GET'</span><span class="token punctuation">,</span> <span class="token string">'POST'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># 登录</span>
<span class="token keyword">def</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    form <span class="token operator">=</span> LoginForm<span class="token punctuation">(</span><span class="token punctuation">)</span>
    emsg <span class="token operator">=</span> <span class="token boolean">None</span>
    <span class="token keyword">if</span> form<span class="token punctuation">.</span>validate_on_submit<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 说明是POST方法</span>
        user_name <span class="token operator">=</span> form<span class="token punctuation">.</span>username<span class="token punctuation">.</span>data
        password <span class="token operator">=</span> form<span class="token punctuation">.</span>password<span class="token punctuation">.</span>data
        user_info <span class="token operator">=</span> get_user<span class="token punctuation">(</span>user_name<span class="token punctuation">)</span>  <span class="token comment"># 从用户数据中查找用户记录（数据库已存在的用户记录）</span>
        <span class="token keyword">if</span> user_info <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>  <span class="token comment"># 并没有此用户</span>
            emsg <span class="token operator">=</span> <span class="token string">&quot;用户名不存在&quot;</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>  <span class="token comment"># 查找到该用户，进入登录验证逻辑</span>
            user <span class="token operator">=</span> User<span class="token punctuation">(</span>user_info<span class="token punctuation">)</span>  <span class="token comment"># 创建用户实体（实例化对象，用于创建session）</span>
            <span class="token keyword">if</span> user<span class="token punctuation">.</span>verify_password<span class="token punctuation">(</span>password<span class="token punctuation">)</span><span class="token punctuation">:</span>  <span class="token comment"># 校验密码</span>
                login_user<span class="token punctuation">(</span>user<span class="token punctuation">)</span>  <span class="token comment"># 创建用户 Session</span>
                <span class="token keyword">return</span> redirect<span class="token punctuation">(</span>request<span class="token punctuation">.</span>args<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">'next'</span><span class="token punctuation">)</span> <span class="token keyword">or</span> url_for<span class="token punctuation">(</span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
            <span class="token keyword">else</span><span class="token punctuation">:</span>
                emsg <span class="token operator">=</span> <span class="token string">&quot;用户名或密码有误&quot;</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'login.html'</span><span class="token punctuation">,</span> form<span class="token operator">=</span>form<span class="token punctuation">,</span> emsg<span class="token operator">=</span>emsg<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>一旦查找到存在该用户，且密码匹配，则我们就调用<code>flask-login</code>的<code>login_user</code>方法，这回根据该用户对象创建对应的<code>session</code>
，返回给客户端。</p> <p><code>session</code>创建成功后，即为登录成功。</p> <p><code>flask-login</code>的工作还依赖于<code>user_loader</code>，必须手动创建加载用户的方法，<code>flask-login</code>才能在每次请求过程中验证该用户。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token decorator annotation punctuation">@login_manager<span class="token punctuation">.</span>user_loader</span>  <span class="token comment"># 定义获取登录用户的方法</span>
<span class="token keyword">def</span> <span class="token function">load_user</span><span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> User<span class="token punctuation">.</span>get<span class="token punctuation">(</span>user_id<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>在此后的每次需要登录的校验中，<code>flask-login</code>都会根据已经创建的<code>session</code>获取<code>User</code>中该用户的id，以进行下一步工作。</p> <h3 id="验证登录"><a href="#验证登录" class="header-anchor">#</a> 验证登录</h3> <p>使用需要登录才能访问的index页面验证登录。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/index'</span><span class="token punctuation">)</span>  <span class="token comment"># 首页</span>
<span class="token decorator annotation punctuation">@login_required</span>  <span class="token comment"># 需要登录才能访问</span>
<span class="token keyword">def</span> <span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># current_user 是当前登录者，是 User 的实例，是 Flask-Login 提供全局变量，类似于g变量</span>
    <span class="token comment"># 这里只有登录成功current_user才会具备实例User的username属性，因此可以成功传递到前端模板</span>
    <span class="token keyword">return</span> render_template<span class="token punctuation">(</span><span class="token string">'index.html'</span><span class="token punctuation">,</span> username<span class="token operator">=</span>current_user<span class="token punctuation">.</span>username<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>如果登录成功，index页面中将会出现欢迎字样：</p> <p><img src="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.assets/img.png" alt="img.png"></p> <h3 id="登出逻辑"><a href="#登出逻辑" class="header-anchor">#</a> 登出逻辑</h3> <p>登出后一般显示登录的界面以示登出成功，也可以自主设计一个登出页面。</p> <p>登出的链接已经在index中部署，下面介绍后端逻辑。</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token decorator annotation punctuation">@app<span class="token punctuation">.</span>route</span><span class="token punctuation">(</span><span class="token string">'/logout'</span><span class="token punctuation">)</span>  <span class="token comment"># 登出</span>
<span class="token decorator annotation punctuation">@login_required</span>  <span class="token comment"># 只有登录了才有必要登出，所以加上注解 @login_required</span>
<span class="token keyword">def</span> <span class="token function">logout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    logout_user<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 登出视图不需要模板，直接跳转到登录页，实际项目中可以增加一个登出页，展示些有趣的东西</span>
    <span class="token keyword">return</span> redirect<span class="token punctuation">(</span>url_for<span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>直接使用<code>logout_user()</code>即可登出。</p> <h3 id="获取登录的用户"><a href="#获取登录的用户" class="header-anchor">#</a> 获取登录的用户</h3> <p>用户登录成功后，如何获取是谁登录的呢？<code>flask-login</code>提供下面方法：</p> <div class="language-python line-numbers-mode"><pre class="language-python"><code><span class="token decorator annotation punctuation">@login_manager<span class="token punctuation">.</span>user_loader</span>  <span class="token comment"># 定义获取登录用户的方法</span>
<span class="token keyword">def</span> <span class="token function">load_user</span><span class="token punctuation">(</span>user_id<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">return</span> User<span class="token punctuation">.</span>get<span class="token punctuation">(</span>user_id<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>大功告成！</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2023/6/24 22:23:41</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#创建虚拟环境" class="sidebar-link reco-side-创建虚拟环境" data-v-cb1513f6>创建虚拟环境</a></li><li class="level-2" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#编写逻辑" class="sidebar-link reco-side-编写逻辑" data-v-cb1513f6>编写逻辑</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#需要引用的包" class="sidebar-link reco-side-需要引用的包" data-v-cb1513f6>需要引用的包</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#加载密钥和初始化登陆对象" class="sidebar-link reco-side-加载密钥和初始化登陆对象" data-v-cb1513f6>加载密钥和初始化登陆对象</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#加载前置用户" class="sidebar-link reco-side-加载前置用户" data-v-cb1513f6>加载前置用户</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#创建用户示例对象" class="sidebar-link reco-side-创建用户示例对象" data-v-cb1513f6>创建用户示例对象</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#编写前端逻辑" class="sidebar-link reco-side-编写前端逻辑" data-v-cb1513f6>编写前端逻辑</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#后端登录逻辑" class="sidebar-link reco-side-后端登录逻辑" data-v-cb1513f6>后端登录逻辑</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#验证登录" class="sidebar-link reco-side-验证登录" data-v-cb1513f6>验证登录</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#登出逻辑" class="sidebar-link reco-side-登出逻辑" data-v-cb1513f6>登出逻辑</a></li><li class="level-3" data-v-cb1513f6><a href="/vuepress-reco-demo/docs/%E7%BC%96%E7%A8%8B%E5%AD%A6%E4%B9%A0/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80/python/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/flask/%E6%89%A9%E5%B1%95%E6%8F%92%E4%BB%B6/flask-login.html#获取登录的用户" class="sidebar-link reco-side-获取登录的用户" data-v-cb1513f6>获取登录的用户</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="Sakura" data-v-248d85d6><canvas id="canvas_sakura" style="z-index:-1;" data-v-248d85d6></canvas></div><!----><div class="RibbonAnimation"></div><div></div><canvas id="vuepress-canvas-cursor"></canvas><APlayer audio="" fixed="true" mini="true" theme="#b7daff" loop="loop" order="random" preload="auto" volume="1" mutex="true" lrc-type="3" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer></div></div>
    <script src="/vuepress-reco-demo/assets/js/app.4bb1badd.js" defer></script><script src="/vuepress-reco-demo/assets/js/3.dedc3bda.js" defer></script><script src="/vuepress-reco-demo/assets/js/1.1f77094c.js" defer></script><script src="/vuepress-reco-demo/assets/js/28.4eef19c0.js" defer></script>
  </body>
</html>
